<?php
namespace Wap\Controller;
 
class OrderController extends CommonController {
	public function index() {
		$this -> isLogin();
		$order_state = I('get.order_state');
		$order = M('order') -> join('tm_order_goods ON tm_order_goods.order_id = tm_order.order_id') -> field('tm_order_goods.goods_name,tm_order_goods.goods_image, tm_order.order_id, tm_order.store_name,tm_order.order_state,tm_order.goods_amount') -> limit(10) -> order("tm_order.order_id DESC");
		if($order_state) {
			$order_data = $order -> where(' tm_order.buyer_id = %s and tm_order.order_state = %s ', $_SESSION['user_id'], $order_state) -> select();
		} else {
			$order_data = $order -> where(' tm_order.buyer_id = ' . $_SESSION['user_id']) -> select();
		}
		$this -> assign('order_state', $order_state);
		$this -> assign('order_data', $order_data);
		$this -> display();
	}
	/*
	 * 退款退货
	 */
	public function back() {
		$this -> isLogin();
		$order_data = M('order') -> join('tm_order_goods ON tm_order_goods.order_id = tm_order.order_id') -> where('tm_order.order_state = 10 and tm_order.buyer_id = ' . $_SESSION['user_id']) -> field('tm_order.store_name, tm_order_goods.goods_name, tm_order_goods.goods_image,tm_order_goods.goods_price') -> select();
		//dump($order_data);exit;
		$this -> assign('orders', $order_data);
		$this -> display();
	}

	/**
	 * 订单提交
	 */
	public function firm_order() {
		$this -> isLogin();
		$product_id = I('get.id');
		$address = M('address');
		$address_id = I('post.address_id');
		if($address_id) {
			$address_data = $address -> find($address_id);
		} else {
			$address_data = $address -> where('is_default = 1 and userid = ' . $_SESSION['user_id']) -> find();
			if(!$address_data) {
				$address_data = $address  -> where('userid = ' . $_SESSION['user_id']) -> order('id DESC') -> select()[0];
			}
		}
		if(IS_POST) {
			$data = I('post.');
			$product = M($this -> tm_product) -> find($data['goods_id']);
			$area_ids = unserialize($product['shop_area_ids']);
			$payment = M('shop_area') -> join('tm_payment ON tm_payment.payment_id = tm_shop_area.pay_id') -> field('tm_payment.payment_code') -> find($area_ids[0]);
			$store = M($this -> tm_store) -> find($product['st_id']);
			$data_order = array();
			$data_order['pay_sn'] = makePaySn($_SESSION['user_id']) ;
			$pay_sn && $pay_id = AddPay_sn('order_pay', $data_order['pay_sn'], '10');
			$data_order['order_sn'] = makeOrderSn($pay_id);
			$data_order['store_id'] = $store['store_id'];
			$data_order['store_name'] = $store['store_name'];
			$data_order['buyer_id'] = $_SESSION['user_id'];
			$data_order['buyer_name'] = $_SESSION['user_name'];
			$data_order['buyer_email'] = 'xx';
			$data_order['add_time'] = time();
			$data_order['payment_code'] = $payment['payment_code'];
			$product['price'] = $data['price'];
			$data_order['goods_amount'] = $data['price'] * $data['goods_num'];
			$data_order['order_amount'] = $data['price'] * $data['goods_num'];
			$data_order['shipping_fee'] = $product['goods_freight'];
			$data_order['order_state'] = 10;
			$data_order['realname'] = $address_data['realname'];
			$data_order['province'] = $address_data['province'];
			$data_order['city'] = $address_data['city'];
			$data_order['area'] = $address_data['area'];
			$data_order['address'] = $address_data['address'];
			$data_order['tel_phone'] = $address_data['tel_phone'];
			$data_order['mob_phone'] = $address_data['mob_phone'];
			$order_id = M('order') -> add($data_order);
			if(!$order_id) {
				$this -> error('订单提交失败');
			}
			$data_order_goods = array();
			$data_order_goods['order_id'] = $order_id;
			$data_order_goods['goods_id'] = $product['id'];
			$data_order_goods['goods_name'] = $product['productname'];
			$data_order_goods['goods_price'] = $data['price'];
			$data_order_goods['goods_num'] = $data['goods_num'];
			$data_order_goods['goods_image'] = $product['pic'];
			$data_order_goods['goods_pay_price'] = $product['price'];
			$data_order_goods['store_id'] = $store['store_id'];
			$data_order_goods['buyer_id'] = $_SESSION['user_id'];
			$data_order_goods['payment_code'] = $payment['payment_code'];
			$data_order_goods['point_num'] = $data['point_num'];
			$data_order_goods['give_coin'] = $product['give_coin'];
			$data_order_goods['coin_num'] = $product['coin_num'];
			$data_order_goods['color_n'] = $data['color_n'];
			$data_order_goods['spec_n'] = $data['spec_n'];
			
			if(M('order_goods') -> add($data_order_goods)) {
				$this -> redirect('/Wap/Order/pay_order?order_id=' . $order_id);
			} else {
				M('order') -> delete($order_id);
				$this -> error('订单提交失败');
			}
		} else if($product_id) {
			$product = M($this -> tm_product) -> find($product_id);
			$store = M($this -> tm_store) -> find($product['st_id']);
			if($store['member_id'] == $_SESSION['user_id']) {
				$this -> error('不能购买自己店铺');
				exit;
			}
			
			$this -> assign('get', I('get.'));
			$this -> assign('product', $product);
			$this -> assign('store', $store);
			$this -> assign('address', $address_data);
			$this -> display();
		} else {
			$this -> error('非法操作');
		}
	}
	
	/**
	 * 删除订单
	 */
	public function delete_order() {
		$this -> isLogin();
		$order_id = I('get.order_id');
		if($order_id) {
			if(M('order') -> where('order_id = ' . $order_id) -> delete() && M('order_goods') -> where('order_id = ' . $order_id) -> delete()) {
				$this -> success('订单删除成功', '/Wap/Order/index');
				exit();
			} else {
				$this -> error('订单删除失败');
			}
		} else {
			$this -> error('非法操作');
		}
	}
	
	/*
	 * 支付订单
	 */
	public function pay_order() {
		$this -> isLogin();
		$order_id = I('get.order_id');
		if($order_id) {
			if(IS_POST) {
				$data = I('post.');
				$user = M($this -> tm_user) -> find($_SESSION['user_id']);
				if(_checkPwd(md5($data['paypassword']), $user['paypassword'])) {
					$this -> error('订单暂时不能手机支付');
				} else {
					$this -> error('支付密码错误');
				}
			}
			$order_data = M('order') -> where('order_id = ' . $order_id) -> find();
			$order_goods_data = M('order_goods') -> where('order_id = ' . $order_id) -> find();
			$this -> assign('order', $order_data);
			$this -> assign('order_goods', $order_goods_data);
			$this -> assign('integral', M($this -> tm_integral) ->  where('userid = ' . $_SESSION['user_id']) -> find());
			$this -> display();
		} else {
			$this -> error('非法操作');
		}
	}
}